using System;
using NUnit.Framework;
using Rhino.Commons;
using WineCellar.Business.Entities;

namespace WineCellar.Data.Tests
{
    [TestFixture]
    public class WineRepositoryFixture :DatabaseFixture
    {
        private IRepository<Wine> wineRepository;
        private User user;
        private Region region;

        [SetUp]
        public override void SetUp()
        {
            base.SetUp();
            wineRepository = IoC.Resolve<IRepository<Wine>>();

            user = new User();
            user.UserName = "bart";
            user.Password = "bart";

            region = new Region();
            region.AddedBy = user;
            region.DateAdded = DateTime.Now;
            region.Name = "Bordeaux";           

            UnitOfWork.CurrentSession.Save(user);
            UnitOfWork.CurrentSession.Save(region);
            UnitOfWork.Current.TransactionalFlush();

        }

        [Test]
        public void CanSaveWine()
        {
            var wine = new Wine();
            wine.AddedBy = user;
            wine.DateAdded = DateTime.Now;
            wine.Appelation = "Pomerol";
            wine.ColorType = "Red";
            wine.Producer = "Lafitte";
            wine.Name = "Chateau Petrus";
            wine.Region = region;

            UnitOfWork.CurrentSession.Save(wine);
            UnitOfWork.CurrentSession.Flush();
            UnitOfWork.CurrentSession.Evict(wine);

            var fromDb = UnitOfWork.CurrentSession.Get<Wine>(wine.Id);
            Assert.IsNotNull(fromDb);
            Assert.AreEqual(wine.Name,fromDb.Name);
        }
    }
}